package com.xmut.mall.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.mall.store.constant.StoreStatusConstant;
import com.xmut.mall.store.dao.StoreInfoDao;
import com.xmut.mall.store.dto.StoreSearchParams;
import com.xmut.mall.store.entity.StoreDetailEntity;
import com.xmut.mall.store.entity.StoreInfoEntity;
import com.xmut.mall.store.feign.CouponFeignService;
import com.xmut.mall.store.service.StoreDetailService;
import com.xmut.mall.store.service.StoreInfoService;
import com.xmut.mall.store.utils.PageUtil;
import com.xmut.mall.store.utils.PageUtils;
import com.xmut.mall.store.utils.Query;
import com.xmut.mall.store.utils.RespBean;
import com.xmut.mall.store.vo.PageDataEntityVo;
import com.xmut.mall.store.vo.PageVO;
import com.xmut.mall.store.vo.StoreBasicInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service("storeInfoService")
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoDao, StoreInfoEntity> implements StoreInfoService {

    @Autowired
    private StoreDetailService storeDetailService;

    @Autowired
    private CouponFeignService couponFeignService;

    /**
     * 检测当前用户是否申请过店铺
     * @param memberId
     * @return
     */
    @Override
    public RespBean checkUserStoreExists(Long memberId) {
        int count = this.count(new QueryWrapper<StoreInfoEntity>().eq("member_id", memberId));
        if(count==0){
            return RespBean.success("该用户还未申请店铺");
        }else {
            return RespBean.error("该用户已申请店铺");
        }

    }

    /**
     * 根据params参数获取商铺审核的分页数据
     * @param params
     * @return
     */
    @Override
    public RespBean getShopAuditPageByParams(Map<String, Object> params) {
        QueryWrapper<StoreInfoEntity> queryWrapper = new QueryWrapper();
        // 查询正在申请中的店铺
        if(!String.valueOf(params.get("storeDisable")).equals("null")){
            queryWrapper.eq("store_status",params.get("storeDisable").toString());
        }
        // 匹配店铺名
        if (!String.valueOf(params.get("storeName")).equals("null") && !String.valueOf(params.get("storeName")).equals("")){
            queryWrapper.like("store_name",params.get("storeName").toString());
        }
        // 通过用户的名称进行匹配
        if(!String.valueOf(params.get("memberName")).equals("null") && !String.valueOf(params.get("memberName")).equals("")){
            queryWrapper.like("member_name",params.get("memberName").toString());
        }
        // 通过日期进行匹配
        if(!String.valueOf(params.get("startDate")).equals("null") && !String.valueOf(params.get("startDate")).equals("")){
            // 进行类型转换
            Date date = new Date();
            try {
                date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(params.get("startDate").toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            queryWrapper.ge("create_time",date);
        }else if(!String.valueOf(params.get("endDate")).equals("null") && !String.valueOf(params.get("endDate")).equals("")){
            // 进行类型转换
            Date date = new Date();
            try {
                date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(params.get("endDate").toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            queryWrapper.le("create_time",date);
        }else if(!String.valueOf(params.get("startDate")).equals("null") && !String.valueOf(params.get("startDate")).equals("")  && !String.valueOf(params.get("endDate")).equals("null") && !String.valueOf(params.get("endDate")).equals("")){
            // 进行类型转换
            Date startDate = new Date();
            Date endDate = new Date();
            try {
                startDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(params.get("startDate").toString());
                endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(params.get("endDate").toString());
            } catch (ParseException e) {
                e.printStackTrace();
            }

            queryWrapper.between("create_time",startDate,endDate);
        }

        // 排序情况
        if(!String.valueOf(params.get("sort")).equals("null")){
            // 判断排序是升序还是降序
            if(String.valueOf(params.get("order")).equals("desc")){
                queryWrapper.orderByDesc(params.get("sort").toString());
            }else {
                queryWrapper.orderByAsc(params.get("sort").toString());
            }
        }


        IPage<StoreInfoEntity> page = this.page(
                new Query<StoreInfoEntity>().getPage(params),
                queryWrapper
        );

        return RespBean.success("获取成功",new PageUtils(page));
    }

    /**
     * 根据用户id获取商铺信息
     * @param memberId
     * @return
     */
    @Override
    public RespBean getShopStatusByMemberId(Long memberId) {
        StoreInfoEntity storeInfo = this.getOne(new QueryWrapper<StoreInfoEntity>().eq("member_id", memberId));
        if(storeInfo!=null){
            if(StoreStatusConstant.STORE_APPLYING.equals(storeInfo.getStoreStatus())){
                // 如果店铺是在申请中
                return RespBean.error("该店铺正在申请中");
            }else if(StoreStatusConstant.STORE_APPLY_OPEN.equals(storeInfo.getStoreStatus())){
                return RespBean.success("开通正常");
            }else if(StoreStatusConstant.STORE_APPLY_REFUSED.equals(storeInfo.getStoreStatus())){
                return RespBean.error("该店铺开通失败,可进行重新申请");
            }else {
                return RespBean.error("网络错误");
            }
        }else {
            return RespBean.error("未开通商铺");
        }
    }

    @Override
    public RespBean getStoreInfoByMemberId(Long memberId) {
        StoreInfoEntity storeInfo = this.getOne(new QueryWrapper<StoreInfoEntity>().eq("member_id", memberId));
        if(storeInfo!=null){
            return RespBean.success("获取成功",storeInfo);
        }else {
            return RespBean.error("该店铺不存在");
        }
    }

    /**
     * 获取卖家店铺详情
     * @param storeId
     * @return
     */
    @Override
    public RespBean getStoreDetail(Long storeId) {
        StoreBasicInfoVO vo = new StoreBasicInfoVO();
        StoreInfoEntity storeInfo = this.getById(storeId);
        StoreDetailEntity storeDetailInfo = storeDetailService.getOne(new QueryWrapper<StoreDetailEntity>()
                .eq("store_id", storeId));
        vo.setStoreId(storeDetailInfo.getStoreId());
        vo.setStoreName(storeInfo.getStoreName());
        vo.setCollectionNum(storeInfo.getCollectionNum());
        vo.setCompanyAddressPath(storeDetailInfo.getCompanyAddressPath());
        vo.setGoodsNum(storeInfo.getGoodsNum());
        Map<String,Object> param = new HashMap<>();
        param.put("type","STORE");
        param.put("clientType","PC");
        param.put("storeId",storeId);
        RespBean pageDataByMapResp = couponFeignService.getPageDataByMap(param);
        PageDataEntityVo pageData = JSON.parseObject(JSON.toJSONString(pageDataByMapResp.getObj()),PageDataEntityVo.class);
        vo.setPcPageData(pageData.getPageData());
        vo.setSelfOperated("1");
        vo.setStoreDesc(storeInfo.getStoreDesc());
        vo.setStoreDisable(storeInfo.getStoreStatus());
        vo.setStoreLogo(storeInfo.getStoreLogo());
        vo.setPageShow(1);

        return RespBean.success("获取成功",vo);
    }

    /**
     * 获取商城店铺列表分页数据
     * @param vo
     * @param params
     * @return
     */
    @Override
    public RespBean getStoreByPage(PageVO vo, StoreSearchParams params) {
        IPage<StoreInfoEntity> page = this.baseMapper.getStoreByPage(PageUtil.initPage(vo), params);

        return RespBean.success("获取成功",page);
    }




































}
